package com.todo.strategy.offline.application.convert;

import com.google.common.collect.Lists;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.EnableTypeEnum;
import com.todo.common.utils.TodoCommonUtils;
import com.todo.strategy.common.enums.MemoSuggestStrategyEnum;
import com.todo.strategy.common.enums.MemoTypeEnum;
import com.todo.strategy.offline.application.entity.bo.MemoTemplateBO;
import com.todo.strategy.offline.application.entity.condition.MemoTemplateConditionBO;
import com.todo.strategy.offline.iface.base.BaseMemoContent;
import com.todo.strategy.offline.iface.request.InsertMemoTemplateReq;
import com.todo.strategy.offline.iface.request.QueryMemoTemplateBaseReq;
import com.todo.strategy.offline.iface.request.UpdateMemoTemplateReq;
import com.todo.strategy.offline.iface.response.MemoTemplateResp;
import com.todo.strategy.offline.iface.response.QueryMemoTemplateBaseResp;
import com.todo.strategy.offline.infrastructure.entity.condition.MemoTemplateCondition;
import com.todo.strategy.offline.infrastructure.entity.domain.MemoTemplateContentDO;
import com.todo.strategy.offline.infrastructure.entity.model.MemoTemplateContentModel;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author azhebuxing
 * @date 2024/11/28 03:10
 * @description
 */
public class MemoTemplateConvert {
    public static MemoTemplateContentDO convertToMemoTemplateContentDO(MemoTemplateBO memoTemplateBO) {
        if (memoTemplateBO == null) {
            return null;
        }
        MemoTemplateContentDO memoTemplateContentDO = new MemoTemplateContentDO();
        memoTemplateContentDO.setIsPlatform(memoTemplateBO.getIsPlatform());
        memoTemplateContentDO.setBaseType(memoTemplateBO.getMemoTypeEnum() == null ? null : memoTemplateBO.getMemoTypeEnum().getType());
        memoTemplateContentDO.setName(memoTemplateBO.getName());
        memoTemplateContentDO.setDesc(memoTemplateBO.getDesc());
        memoTemplateContentDO.setReferenceOpinion(memoTemplateBO.getReferenceOpinion());
        memoTemplateContentDO.setSuggestStrategy(memoTemplateBO.getSuggestStrategy() == null ? null : memoTemplateBO.getSuggestStrategy().getType());
        memoTemplateContentDO.setContentModel(memoTemplateBO.getContentModel());
        memoTemplateContentDO.setUserId(memoTemplateBO.getUserId());
        memoTemplateContentDO.setStatus(memoTemplateBO.getStatus() == null ? null : memoTemplateBO.getStatus().getType());
        memoTemplateContentDO.setUseCount(memoTemplateBO.getUseCount());
        memoTemplateContentDO.setId(memoTemplateBO.getId());
        memoTemplateContentDO.setIsDelete(memoTemplateBO.getIsDelete());
        memoTemplateContentDO.setCreateTime(memoTemplateBO.getCreateTime());
        memoTemplateContentDO.setUpdateTime(memoTemplateBO.getUpdateTime());
        return memoTemplateContentDO;
    }

    public static MemoTemplateBO convertToMemoTemplateBO(MemoTemplateContentDO memoTemplateContentDO) {
        if (memoTemplateContentDO == null) {
            return null;
        }
        MemoTemplateBO memoTemplateBO = new MemoTemplateBO();
        memoTemplateBO.setIsPlatform(memoTemplateContentDO.getIsPlatform());
        memoTemplateBO.setMemoTypeEnum(MemoTypeEnum.getByType(memoTemplateContentDO.getBaseType()));
        memoTemplateBO.setName(memoTemplateContentDO.getName());
        memoTemplateBO.setDesc(memoTemplateContentDO.getDesc());
        memoTemplateBO.setReferenceOpinion(memoTemplateContentDO.getReferenceOpinion());
        memoTemplateBO.setSuggestStrategy(MemoSuggestStrategyEnum.getByType(memoTemplateContentDO.getSuggestStrategy()));
        memoTemplateBO.setContentModel(memoTemplateContentDO.getContentModel());
        memoTemplateBO.setUserId(memoTemplateContentDO.getUserId());
        memoTemplateBO.setStatus(EnableTypeEnum.getByType(memoTemplateContentDO.getStatus()));
        memoTemplateBO.setUseCount(memoTemplateContentDO.getUseCount());
        memoTemplateBO.setId(memoTemplateContentDO.getId());
        memoTemplateBO.setIsDelete(memoTemplateContentDO.getIsDelete());
        memoTemplateBO.setCreateTime(memoTemplateContentDO.getCreateTime());
        memoTemplateBO.setUpdateTime(memoTemplateContentDO.getUpdateTime());
        return memoTemplateBO;
    }

    public static MemoTemplateBO convertToMemoTemplateBO(InsertMemoTemplateReq req) {
        if (req == null || req.get_staff() == null) {
            return null;
        }
        MemoTemplateBO memoTemplateBO = new MemoTemplateBO();
        memoTemplateBO.setId(TodoCommonUtils.buildLongGuid());
        memoTemplateBO.setMemoTypeEnum(MemoTypeEnum.getByType(req.getMemoType()));
        memoTemplateBO.setSuggestStrategy(MemoSuggestStrategyEnum.getByType(req.getSuggestStrategy()));
        memoTemplateBO.setContentModel(convertToMemoTemplateContentModel(req.getMemoContent()));
        memoTemplateBO.setUserId(req.get_staff().getUserId());
        memoTemplateBO.setName(req.getName());
        memoTemplateBO.setDesc(req.getDesc());
        memoTemplateBO.setReferenceOpinion(req.getReferenceOpinion());
        memoTemplateBO.setIsPlatform(true);
        memoTemplateBO.setStatus(EnableTypeEnum.FORBIDDEN);
        memoTemplateBO.setUseCount(0);
        memoTemplateBO.setIsDelete(0);
        memoTemplateBO.setCreateTime(new Date());
        memoTemplateBO.setUpdateTime(new Date());
        return memoTemplateBO;
    }

    private static MemoTemplateContentModel convertToMemoTemplateContentModel(BaseMemoContent memoContent) {
        if (memoContent == null || CollectionUtils.isEmpty(memoContent.getModuleClassifyList())) {
            return null;
        }
        MemoTemplateContentModel model = new MemoTemplateContentModel();
        List<MemoTemplateContentModel.ModuleClassify> moduleClassifyList = new ArrayList<>();
        for (BaseMemoContent.ModuleClassify moduleClassify : memoContent.getModuleClassifyList()) {
            MemoTemplateContentModel.ModuleClassify newModuleClassifyModel = new MemoTemplateContentModel.ModuleClassify();
            newModuleClassifyModel.setModuleName(moduleClassify.getModuleName());
            newModuleClassifyModel.setSort(moduleClassify.getSort());
            if (CollectionUtils.isNotEmpty(moduleClassify.getItemList())) {
                List<MemoTemplateContentModel.Item> itemList = new ArrayList<>();
                for (BaseMemoContent.Item item : moduleClassify.getItemList()) {
                    MemoTemplateContentModel.Item newItem = new MemoTemplateContentModel.Item();
                    newItem.setItemName(item.getItemName());
                    newItem.setSort(item.getSort());
                    newItem.setDescribe(item.getDescribe());
                    itemList.add(newItem);
                }
                newModuleClassifyModel.setItemList(itemList);
            }
            moduleClassifyList.add(newModuleClassifyModel);
        }
        model.setModuleClassifyList(moduleClassifyList);
        return model;
    }

    public static MemoTemplateBO convertToMemoTemplateBO(UpdateMemoTemplateReq req) {
        if (req == null || req.get_staff() == null || req.getId() == null) {
            return null;
        }
        MemoTemplateBO memoTemplateBO = new MemoTemplateBO();
        memoTemplateBO.setId(req.getId());
        memoTemplateBO.setMemoTypeEnum(MemoTypeEnum.getByType(req.getMemoType()));
        memoTemplateBO.setSuggestStrategy(MemoSuggestStrategyEnum.getByType(req.getSuggestStrategy()));
        memoTemplateBO.setContentModel(convertToMemoTemplateContentModel(req.getMemoContent()));
        memoTemplateBO.setName(req.getName());
        memoTemplateBO.setDesc(req.getDesc());
        memoTemplateBO.setReferenceOpinion(req.getReferenceOpinion());
        memoTemplateBO.setUpdateTime(new Date());
        return memoTemplateBO;
    }

    public static MemoTemplateCondition convertToMemoTemplateCondition(MemoTemplateConditionBO memoTemplateConditionBO) {
        if (memoTemplateConditionBO == null) {
            return null;
        }
        MemoTemplateCondition memoTemplateCondition = new MemoTemplateCondition();
        memoTemplateCondition.setMemoTypeEnum(memoTemplateConditionBO.getMemoTypeEnum() == null ? null : memoTemplateConditionBO.getMemoTypeEnum().getType());
        memoTemplateCondition.setSuggestStrategy(memoTemplateConditionBO.getSuggestStrategy() == null ? null : memoTemplateConditionBO.getSuggestStrategy().getType());
        memoTemplateCondition.setStatus(memoTemplateConditionBO.getStatus() == null ? null : memoTemplateConditionBO.getStatus().getType());
        memoTemplateCondition.setUserId(memoTemplateConditionBO.getUserId());
        memoTemplateCondition.setPageSize(memoTemplateConditionBO.getPageSize());
        memoTemplateCondition.setPageIndex(memoTemplateConditionBO.getPageIndex());
        memoTemplateCondition.setCurrentIndex(memoTemplateConditionBO.getCurrentIndex());
        memoTemplateCondition.setName(memoTemplateConditionBO.getName());
        return memoTemplateCondition;
    }

    public static MemoTemplateConditionBO convertToMemoTemplateConditionBO(QueryMemoTemplateBaseReq req) {
        if (req == null) {
            return null;
        }
        MemoTemplateConditionBO conditionBO = new MemoTemplateConditionBO();
        conditionBO.setPageSize(req.getPageSize());
        conditionBO.setPageIndex(req.getPageIndex());
        conditionBO.setCurrentIndex(req.getCurrentIndex());
        conditionBO.setName(req.getName());
        conditionBO.setMemoTypeEnum(MemoTypeEnum.getByType(req.getMemoTypeEnum()));
        conditionBO.setSuggestStrategy(MemoSuggestStrategyEnum.getByType(req.getSuggestStrategy()));
        conditionBO.setStatus(EnableTypeEnum.getByType(req.getStatus()));
        conditionBO.setUserId(req.getUserId());
        return conditionBO;
    }

    public static MemoTemplateResp convertToMemoTemplateResp(MemoTemplateBO memoTemplateBO) {
        if (memoTemplateBO == null) {
            return null;
        }
        MemoTemplateResp resp = new MemoTemplateResp();
        resp.setId(memoTemplateBO.getId());
        resp.setMemoType(memoTemplateBO.getMemoTypeEnum() == null ? null : memoTemplateBO.getMemoTypeEnum().getType());
        resp.setMemoTypeStr(memoTemplateBO.getMemoTypeEnum() == null ? null : memoTemplateBO.getMemoTypeEnum().getName());
        resp.setSuggestStrategy(memoTemplateBO.getSuggestStrategy() == null ? null : memoTemplateBO.getSuggestStrategy().getType());
        resp.setSuggestStrategyStr(memoTemplateBO.getSuggestStrategy() == null ? null : memoTemplateBO.getSuggestStrategy().getName());
        resp.setStatus(memoTemplateBO.getStatus() == null ? null : memoTemplateBO.getStatus().getType());
        resp.setDesc(memoTemplateBO.getDesc());
        resp.setName(memoTemplateBO.getName());
        resp.setReferenceOpinion(memoTemplateBO.getReferenceOpinion());
        resp.setUseCount(memoTemplateBO.getUseCount());
        resp.setCreateTime(memoTemplateBO.getCreateTime());
        resp.setUpdateTime(memoTemplateBO.getUpdateTime());
        resp.setMemoContent(convertToMemoContent(memoTemplateBO.getContentModel()));
        resp.setUserId(memoTemplateBO.getUserId());
        resp.setIsPlatform(memoTemplateBO.getIsPlatform());
        return resp;
    }

    private static BaseMemoContent convertToMemoContent(MemoTemplateContentModel contentModel) {
        if (contentModel == null || CollectionUtils.isEmpty(contentModel.getModuleClassifyList())) {
            return null;
        }
        BaseMemoContent model = new BaseMemoContent();
        List<BaseMemoContent.ModuleClassify> moduleClassifyList = new ArrayList<>();
        for (MemoTemplateContentModel.ModuleClassify moduleClassify : contentModel.getModuleClassifyList()) {
            BaseMemoContent.ModuleClassify newModuleClassifyModel = new BaseMemoContent.ModuleClassify();
            newModuleClassifyModel.setModuleName(moduleClassify.getModuleName());
            newModuleClassifyModel.setSort(moduleClassify.getSort());
            if (CollectionUtils.isNotEmpty(moduleClassify.getItemList())) {
                List<BaseMemoContent.Item> itemList = new ArrayList<>();
                for (MemoTemplateContentModel.Item item : moduleClassify.getItemList()) {
                    BaseMemoContent.Item newItem = new BaseMemoContent.Item();
                    newItem.setItemName(item.getItemName());
                    newItem.setSort(item.getSort());
                    newItem.setDescribe(item.getDescribe());
                    itemList.add(newItem);
                }
                newModuleClassifyModel.setItemList(itemList);
            }
            moduleClassifyList.add(newModuleClassifyModel);
        }
        model.setModuleClassifyList(moduleClassifyList);
        return model;
    }

    public static PageResult<QueryMemoTemplateBaseResp> convertToPageQueryMemoTemplateBaseResp(PageResult<MemoTemplateBO> pageResult) {
        if (pageResult == null) {
            return null;
        }
        if (CollectionUtils.isEmpty(pageResult.getResults())) {
            return new PageResult<>(Lists.newArrayList(), pageResult.getPageNum(), pageResult.getPageSize(), pageResult.getTotal());
        }
        List<QueryMemoTemplateBaseResp> respList = new ArrayList<>();
        for (MemoTemplateBO memoTemplateBO : pageResult.getResults()) {
            QueryMemoTemplateBaseResp resp = new QueryMemoTemplateBaseResp();
            resp.setId(memoTemplateBO.getId());
            resp.setMemoType(memoTemplateBO.getMemoTypeEnum() == null ? null : memoTemplateBO.getMemoTypeEnum().getType());
            resp.setMemoTypeStr(memoTemplateBO.getMemoTypeEnum() == null ? null : memoTemplateBO.getMemoTypeEnum().getName());
            resp.setSuggestStrategy(memoTemplateBO.getSuggestStrategy() == null ? null : memoTemplateBO.getSuggestStrategy().getType());
            resp.setSuggestStrategyStr(memoTemplateBO.getSuggestStrategy() == null ? null : memoTemplateBO.getSuggestStrategy().getName());
            resp.setStatus(memoTemplateBO.getStatus() == null ? null : memoTemplateBO.getStatus().getType());
            resp.setName(memoTemplateBO.getName());
            resp.setUseCount(memoTemplateBO.getUseCount());
            resp.setUserId(memoTemplateBO.getUserId());
            resp.setIsPlatform(memoTemplateBO.getIsPlatform());
            respList.add(resp);
        }
        return PageResult.of(respList, pageResult.getPageNum(), pageResult.getPageSize(), pageResult.getTotal());
    }
}

